var pro_load_flag = true,
	pro_load_page = 2;
var bill ={
	init : function(){
		app.ajax(
			'/Member/Index/my_bill',
			null,
			function(res){
				$('#tpl-bill-balance').hide();
//				$('.bill-list-balance').hide();
				app.tpl($('.bill-list'),'tpl-bill-welfare',res);
			}
		);
	},
	welfare :function(){
		app.ajax(
			'/Member/Index/cash_list',
			{page :1},
			function(res){
				$('#tpl-bill-welfare').hide();
//				$('.bill-list-welfare').hide();
				app.tpl($('.bill-list'),'tpl-bill-balance',res);
			}
		);
	},
	loadMore: function(url){
		var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;" class="no-more">努力加载中~<div>';
   		$('.bill-list').append(listLoadingEmpty);
		pro_load_flag = false;
		app.ajax(
			url,
			{
				page : pro_load_page,
				limit :20
			},
			function(res){
				$('.bill-list .no-more').remove();
				if(res.code == 200){
					var list=res.list;
					var type = window.sessionStorage.getItem('billType');
					if(list){
						if(type == 0){
							for(var i=0,html='';i< list.length;i++){
									var classN = '';
									var  pText = '（已领取）';
									var time = new Date(list[i].return_time);
									time =time.getFullYear() + '-' + time.getMonth()+1 +'-' + time.getDate() + ' ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();
								if(list[i].status == 0){
									classN = 'cDarkRed';
									pText = '（已过期）';
								}
								html+="<div class='mui-row bB'><div class='mui-col-xs-9 pL17 pT17 pB19'><div class='fS18 mB9'>新用户首单满99全返活动</div>"
										+"<p class='fS12 cGrey '>"+time+"</p></div>"
										+"<div class='mui-col-xs-3 pT19 pB19 fS18 tC'><div class='fS18 mB9"+classN+"'>+"+list[i].returned_money+"</div>"
										+"<p class='fS12 cGrey '>"+pText+"</p></div></div>";
							}
							$('。bill-welfare-list').append(html);
						}else{
							for(var i=0,html='';i< list.length;i++){
									var classN = '';
									var  pText = '（已领取）';
								if(list[i].status == 0){
									classN = 'cDarkRed';
									pText = '（已过期）';
								}
								html+="<div class='mui-row bB'><div class='mui-col-xs-9 pL19 pT19 pB19'><div class='fS18 mB11'>"+list[i].description+"</div>"
										+"<p class='fS12 cGrey '>"+list[i].create_time+"</p></div><div class='mui-col-xs-3 pR16 lH79 tR fS18'>"+list[i].price+"</div></div>";
							}
							$('。bill-balance-list').append(html);
						}
							pro_load_page++;
							pro_load_flag = true;
					}else{
						var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;">没有更多数据了~<div>';
						$('.product-list').append(listLoadingEmpty);
					}
				}else{
					layer.msg('系统繁忙，请稍候再试');
				}
			}
		);	
	}
}

footer.init();
header.init('账单');
bill.init();
window.sessionStorage.setItem('billType',0);
window.onload = function(){
	if($('header').html()){
		$('.bill-list').css('marginTop','7.34rem');
	}
	$('.bill-nav li:first i').show();
		
	$('body').on('click','.bill-nav li',function(){
		$('.bill-nav li h2').removeClass('font-active');
		$('.bill-nav li i').hide();
		$(this).find('h2').addClass('font-active');
		$(this).find('i').show();
		var type=$(this).attr('data-type');
		var billType = window.sessionStorage.getItem('billType');
		if(type == billType){return false;}
		if(type == 0){
			window.sessionStorage.setItem('billType',type);
			bill.init();
		}else if(type == 1){
			window.sessionStorage.setItem('billType',type);
			bill.welfare();
		}
		pro_load_flag = true;
		$('.bill-list .no-more').remove();
	});
	
	$(window).scroll(function(){
				// 当滚动到距底部以上200像素
				var offset = $(".bill-list").offset(),
				dataUrl='';
				var documentHeight = offset.top + $(".bill-list").height();
				if (documentHeight - $(this).scrollTop() - $(this).height()<700){
					var type = window.sessionStorage.getItem('billType');
					if(type == 0){
						dataUrl = '/Member/Index/my_bill';
					}else{
						dataUrl = '/Member/Index/cash_list';
					}
					pro_load_flag && bill.loadMore(dataUrl);
				}
		});
}
